package b2.scene.camp
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.fort.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class Fortress extends Sprite
	{
		private var fortIndex:int;
		private var requireLevel:TextField;
		
		public function Fortress(fortIndex:int, topName:Boolean = false)
		{
			super();
			this.fortIndex = fortIndex;
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortIndex];
			//加载要塞图标
			var FortressIcon:Class = ResourceHelper.getInstance().getClassByName(fortressConfig.iconImage);
			var fortressIcon:Sprite = new FortressIcon();
			addChild(fortressIcon);
			//加载要塞名字
			var fortressName:TextField = new TextField();
			fortressName.selectable = false;
        	fortressName.autoSize = TextFieldAutoSize.CENTER;
    		fortressName.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000, true);
			fortressName.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			fortressName.htmlText = fortressConfig.name;
			fortressName.x = (this.width - fortressName.width) / 2;
			if (topName)
				fortressName.y = -fortressName.height - 5;
			else
				fortressName.y = this.height + 5;
			addChild(fortressName);
			//需要等级
			requireLevel = new TextField();
			requireLevel.selectable = false;
        	requireLevel.autoSize = TextFieldAutoSize.CENTER;
    		requireLevel.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFF0000);
    		requireLevel.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			requireLevel.text = "需要等级" + fortressConfig.requireLevel;
			requireLevel.x = (this.width - requireLevel.width) / 2;
			requireLevel.y = fortressName.y + 20;
			addChild(requireLevel);
			
			refresh();
			
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		public function refresh():void
		{
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortIndex];
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (fortressConfig.requireLevel > me.level)
				requireLevel.visible = true;
			else
				requireLevel.visible = false;
		}

		//是否可进入要塞
		public function isAbleToGo():Boolean
		{
			return !requireLevel.visible;
		}

		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}

		private function onMouseClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortIndex];
			if (fortressConfig.requireLevel > me.level)
			{
	        	var popMessage:PopMessage = new PopMessage("等级不足，还不能进入该要塞！", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				var fortressDialog:FortressDialog = new FortressDialog(fortIndex);
				fortressDialog.x = 70;
				fortressDialog.y = (BFont.stageHeight - fortressDialog.height) / 2;
				WidgetManager.getInstance().popOut([fortressDialog], true);
				GuideScene.getInstance().step++;
				GuideScene.getInstance().addClickableWidget(fortressDialog.campInfoPane.campDetailTab.quickGameButton);
			}
		}
	}
}